package com.binvshe.binvshe.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.AccountManager;
import com.binvshe.binvshe.common.LoginEvent;
import com.binvshe.binvshe.message.dao.MessageHelper;
import com.binvshe.binvshe.mine.utils.FileUtils;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.frameworkbase.customviews.iconcontent.IconTextArrowButton;
import com.pachong.android.frameworkbase.dialog.AlertDialogFragment;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import butterknife.Bind;
import butterknife.OnClick;

public class SettingActivity extends BaseActivity {

    @Bind(R.id.mItbtnClearcache)
    IconTextArrowButton mItbtnClearcache;
    @Bind(R.id.mItbtnFeedback)
    IconTextArrowButton mItbtnFeedback;
    @Bind(R.id.mItbtnAboutModo)
    IconTextArrowButton mItbtnAboutModo;
    @Bind(R.id.mBtnSignOut)
    Button mBtnSignOut;

    public static void start(Activity activity) {
        Intent starter = new Intent(activity, SettingActivity.class);
        activity.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getCustomToolbar().setTitle("设置");
        enableBackButton();
        if (AccountManager.getCurrentAccount() == null) {
            mBtnSignOut.setVisibility(View.GONE);
        }
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.act_setting, null);
    }

    @Override
    protected void onResume() {
        super.onResume();
        setCacheSize();
        if (!AccountManager.isTokenValid()) {
            mBtnSignOut.setClickable(false);
        }
    }

    private void setCacheSize() {
        File cacheDir = getCacheDir();
        String cacheSize = FileUtils.size(cacheDir);
        mItbtnClearcache.setSubText(cacheSize);
    }

    @OnClick({R.id.mItbtnClearcache, R.id.mItbtnFeedback, R.id.mItbtnAboutModo, R.id.mBtnSignOut})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.mItbtnClearcache:
                //清除缓存
                final File cacheDir = getCacheDir();
                final String cacheSize = FileUtils.size(cacheDir);
                //弹出Dialog
                showCacheDialog(cacheDir, cacheSize);
                break;
            case R.id.mItbtnFeedback:
                //意见反馈
                FeedbackActivity.start(this);
                break;
            case R.id.mItbtnAboutModo:
                //关于萌都
                AboutModoActivity.start(this);
                break;
            case R.id.mBtnSignOut:
                //退出登录
                final AlertDialogFragment singOutDialogFrag = new AlertDialogFragment();
                singOutDialogFrag.setMessage("确认退出?")
                        .setNegativeButton("取消", true, new View.OnClickListener() {

                            @Override
                            public void onClick(View v) {
                                singOutDialogFrag.dismiss();
                            }
                        })
                        .setPositiveButton("确定", false, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //删除TokenInfo
                                AccountManager.clear();
                                //删除消息列表数据库
                                MessageHelper.getInstance().deleteMessageAll();
                                singOutDialogFrag.dismiss();
                                EventBus.getDefault().post(new LoginEvent(LoginEvent.ACTION_LOGOUT));
                                finish();
                            }
                        }).show(getSupportFragmentManager());
                break;
        }
    }

    private void showCacheDialog(final File cacheDir, final String cacheSize) {
        if ("0B".equals(cacheSize)) {
            final AlertDialogFragment nullCacheDialogFrag = new AlertDialogFragment();
            nullCacheDialogFrag.setMessage("无缓存")
                    .setPositiveButton("确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    nullCacheDialogFrag.dismiss();
                        }
                    }).show(getSupportFragmentManager());
        } else {
            final AlertDialogFragment cleanDialogFrag = new AlertDialogFragment();
            cleanDialogFrag.setMessage("确认清除" + cacheSize + "缓存?")
                    .setNegativeButton("取消", new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            cleanDialogFrag.dismiss();
                        }
                    })
                    .setPositiveButton("确定", new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            FileUtils.delFilesFromPath(cacheDir);
                            mItbtnClearcache.setSubText("0B");
                            //删除消息列表数据库
                            MessageHelper.getInstance().deleteMessageAll();
                            cleanDialogFrag.dismiss();
                            Toast.makeText(getBaseContext(), "已清除" + cacheSize + "缓存!", Toast.LENGTH_SHORT).show();
                        }
                    })
                    .show(getSupportFragmentManager());
        }
    }
}
